<?php

class BinaryNode{
    
    const ERROR = 'ERROR';
    
    private $id;
    private $hash;
    private $path;
    private $is_folder;
    private $parent_id;
    private $pure_path;
      
    /**
     * Constructor
     * @param DB result $row
     */
    public function __construct($row=null) {
        if(!empty($row)){
            $this->set_by_rowdb($row);
        }
    }
    
    /**
     * Set all fields
     * @param int $id
     * @param string $hash
     * @param string $path
     * @param int $is_folder
     * @param int $parent_id
     * @param string $pure_path
     * @return \BinaryNode
     */
    public function set_all($id, $hash, $path, $is_folder, $parent_id, $pure_path){
        $this->id = $id;
        $this->hash = $hash;
        $this->path = $path;
        $this->is_folder = $is_folder;
        $this->parent_id = $parent_id;
        $this->pure_path = $pure_path;
        return $this;
    }
    
    /**
     * Get all fields by Array()
     * @return array
     */
    public function get_all_fields(){
        return array('id' => $this->id, 'hash' => $this->hash, 'path' => $this->path, 'is_folder' => $this->is_folder, 
            'parent_id' => $this->parent_id, 'pure_path' => $this->pure_path);
    }
    
    /**
     * Set all fields by DB array
     * @param array $row
     * @return \BinaryNode
     */
    public function set_by_rowdb($row){
        $this->id = $row['R_ID'];
        $this->hash = $row['R_HASH'];
        $this->path = $row['R_PATH'];
        $this->is_folder = $row['IS_FOLDER'];
        $this->parent_id = $row['PARENT_ID'];
        $this->pure_path = $row['PURE_PATH'];
        return $this;
    }
    
    /**
     * Is empty object?
     * @return boolean
     */
    public function is_empty(){
        return empty($this->hash);
    }
    
    private function parse_title($title){
        $ci_config = new CI_Config();
        $patterns = $ci_config->item('title_patterns');
        $replaces = $ci_config->item('title_replaces');
        
        if (false === strpos($title, $ci_config->item("path_dir_rrd"))) {
            $str = preg_replace($patterns, $replaces, $title);
        } else {
            $str = preg_replace($patterns, $replaces, substr($this->get_path(), strlen($ci_config->item("path_dir_rrd")) + 1));
        }
        return strlen($str) > 0 ? $str : $ci_config->item('empty_string');
    }
    
    public function get_title_full(){
        return $this->parse_title($this->path);
    }
    
    public function get_title_short(){
        $str = substr($this->path, strrpos($this->path,"/")+1);
        return $this->parse_title($str);
    }
    
    public function get_title_cutter($cut_level){
        $ci_config = new CI_Config();
        $path = substr($this->path, strlen($ci_config->item("path_dir_rrd"))+1);
        $pt = explode ("/", $path, $cut_level+1);
        return $this->parse_title($pt[count($pt)-1]);
    }
    
    /**
     * Curent node is root? (hash == 0)
     * @return boolean
     */
    public function is_root(){
        return ($this->hash === '0');
    }
    
    /**
     * Curent node is first? node after root
     * @return boolean
     */
    public function is_first(){
        return ($this->parent_id == 0);
    }
    
    /**
     * Curent node is folder?
     * @return boolean
     */
    public function is_folder(){
        return (boolean)$this->is_folder;
    }
    
    public function get_id() {
        return $this->id;
    }

    public function get_hash() {
        return $this->hash;
    }

    public function get_path() {
        return $this->path;
    }

    public function get_is_folder() {
        return $this->is_folder;
    }

    public function get_parent_id() {
        return $this->parent_id;
    }

    public function get_pure_path() {
        return $this->pure_path;
    }

    public function set_id($id) {
        $this->id = $id;
    }

    public function set_hash($hash) {
        $this->hash = $hash;
    }

    public function set_path($path) {
        $this->path = $path;
    }

    public function set_is_folder($is_folder) {
        $this->is_folder = $is_folder;
    }

    public function set_parent_id($parent_id) {
        $this->parent_id = $parent_id;
    }

    public function set_pure_path($pure_path) {
        $this->pure_path = $pure_path;
    }
}